
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.AppException;
import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 编辑消息请求类
 * 
 * @author liux
 * @since 2013-12-25 下午4:54:22
 */
public class EditMessageTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    /**
     * 编辑消息（删除消息等）
     * 
     * @param appId 本应用的appId
     * @param username
     * @param action
     * @param messageIds
     * @param password 登陆密码或kugouToken
     * @param source 0=使用登陆密码，1=使用酷狗token
     * @param tAppId 和kugouToken对应的appId
     * @return
     */
    public EditMessageHttpResponse request(String username, String action, String messageIds,
            int page, int pageSize) {
        try {
            ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
            // 生成请求包
            HttpBaseRequestPackage req = new HttpBaseRequestPackage();
            req.setUserName(username);
            // 生成响应包
            ResponsePackage resp = new EditMessageHttpResponsePackage();
            // 生成响应类
            EditMessageHttpResponse appsHttpResponse = new EditMessageHttpResponse();
            // 填充参数，key-value。key是接口要求传的变量名称
            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("cmd", ProtocolCmd.CMD_MESSAGE_ACTION);
            params.put("action", action);
            params.put("messageids", messageIds);
            params.put("username", username);
            params.put("page", page);
            params.put("pagesize", pageSize);
            requestParams.add(params);
            req.setRequestParams(requestParams);
            mCmdList.add(ProtocolCmd.CMD_MESSAGE_ACTION);
            KGHttpClient.request(req, resp);
            // 响应类赋值
            resp.getResponseData(appsHttpResponse);
            // 返回响应类
            return appsHttpResponse;
        } catch (AppException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 响应包
    class EditMessageHttpResponsePackage extends HttpBaseResponsePackage<EditMessageHttpResponse> {

        @Override
        protected void handleResponse(EditMessageHttpResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "EditMessageTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    response.setCode(jsonResp.optString("code"));
                    response.setMessage(jsonResp.optString("message"));
                    response.setPrompt(jsonResp.optString("prompt"));
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    if (ProtocolCmd.CMD_MESSAGE_ACTION.equalsIgnoreCase(cmd)) {
                        JSONObject o = jsonResp.getJSONObject("data");
                    }
                    response.setOk(true);
                }
            } catch (Exception e) {
            }

        }
    }

    /**
     * 响应类，这个类主要用于存储返回数据中的各个字段。
     */
    public class EditMessageHttpResponse extends ResponseMessage {

    }
}
